package com.bs.process.project.service.impl;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.bs.common.utils.DateUtils;
import com.bs.common.utils.QRCodeUtils;
import com.bs.common.utils.StringUtils;
import com.bs.framework.util.ShiroUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bs.process.project.mapper.SysProjectFileMapper;
import com.bs.process.project.mapper.SysProjectFileTypeMapper;
import com.bs.process.project.mapper.SysProjectMapper;
import com.bs.process.project.domain.SysProject;
import com.bs.process.project.domain.SysProjectFile;
import com.bs.process.project.domain.SysProjectFileType;
import com.bs.process.project.service.ISysProjectService;
import com.bs.common.config.Global;
import com.bs.common.core.text.Convert;

/**
 * 工地项目Service业务层处理
 *
 * @author Xianlu Tech
 * @date 2020-06-13
 */
@Service
public class SysProjectServiceImpl implements ISysProjectService {
	@Autowired
	private SysProjectMapper sysProjectMapper;
	@Autowired
	private SysProjectFileTypeMapper sysProjectFileTypeMapper;
	@Autowired
	private SysProjectFileMapper sysProjectFileMapper;

	/**
	 * 查询工地项目
	 *
	 * @param projectId 工地项目ID
	 * @return 工地项目
	 */
	@Override
	public SysProject selectSysProjectById(Long projectId) {
		return sysProjectMapper.selectSysProjectById(projectId);
	}

	/**
	 * 查询工地项目列表
	 *
	 * @param sysProject 工地项目
	 * @return 工地项目
	 */
	@Override
	public List<SysProject> selectSysProjectList(SysProject sysProject) {
		return sysProjectMapper.selectSysProjectList(sysProject);
	}

	/**
	 * 新增工地项目
	 *
	 * @param sysProject 工地项目
	 * @return 结果
	 */
	@Override
	public int insertSysProject(SysProject sysProject) {
		sysProject.setCreateBy(ShiroUtils.getLoginName());
		sysProject.setCreateTime(DateUtils.getNowDate());
		sysProject.setProjectCode(proCode());
		int res = sysProjectMapper.insertSysProject(sysProject);
		if (res > 0) {
			long projectId = sysProject.getProjectId();
			
			String content = Global.getProjectQRUrl() + projectId; // 这里设置自定义网站url
//			String logoPath = this.getClass().getResource("/static/img/qrlogo.png").getPath();
			InputStream logoPath = this.getClass().getResourceAsStream("/static/img/qrlogo.png");
			File logoFile = new File("qr.png");
			QRCodeUtils.inputstreamtofile(logoPath, logoFile);
			System.out.println("二维码logo图片地址： "+logoPath);
			String destPath = Global.getProfile()+Global.projectQRCode_dir;
			String qrname = projectId + "";
			String base64 = null;
			try {
				base64 = QRCodeUtils.encodeBase64(qrname, content, logoFile, destPath, true);
			} catch (Exception e) {
				e.printStackTrace();
			}
			sysProject.setQrCode(base64);
			sysProjectMapper.updateSysProject(sysProject);
			//添加项目文件文件
			List<SysProjectFileType> typeList = sysProjectFileTypeMapper.selectSysProjectFileTypeList(new SysProjectFileType());
			for (SysProjectFileType type : typeList) {
				//如果是文件是空的那就是最上层
				long parentId = 0;
				String typeCode = type.getId();
				String parentCode = type.getParentId();
				if(StringUtils.isEmpty(parentCode)) {
					SysProjectFile sysProjectFile = new SysProjectFile();
					sysProjectFile.setProjectId(projectId);
					sysProjectFile.setCreateBy(ShiroUtils.getLoginName());
					sysProjectFile.setCreateTime(DateUtils.getNowDate());
					sysProjectFile.setUpdateBy(ShiroUtils.getLoginName());
					sysProjectFile.setUpdateTime(DateUtils.getNowDate());
					sysProjectFile.setFileType(type.getId());
					sysProjectFile.setFileName(type.getTypename());
					sysProjectFile.setParentId(parentId);
					sysProjectFile.setStatus(0);
					sysProjectFile.setUrl("#");
					sysProjectFileMapper.insertSysProjectFile(sysProjectFile);
					parentId = sysProjectFile.getId();
					for (SysProjectFileType fp1 : typeList) {
						if(typeCode.equalsIgnoreCase(fp1.getParentId())){
							SysProjectFile sysProjectFile2 = new SysProjectFile();
							sysProjectFile2.setProjectId(projectId);
							sysProjectFile2.setCreateBy(ShiroUtils.getLoginName());
							sysProjectFile2.setCreateTime(DateUtils.getNowDate());
							sysProjectFile2.setUpdateBy(ShiroUtils.getLoginName());
							sysProjectFile2.setUpdateTime(DateUtils.getNowDate());
							sysProjectFile2.setFileType(fp1.getId());
							sysProjectFile2.setFileName(fp1.getTypename());
							sysProjectFile2.setParentId(parentId);
							sysProjectFile2.setStatus(0);
							sysProjectFile2.setUrl("#");
							sysProjectFileMapper.insertSysProjectFile(sysProjectFile2);
						}
					}
				}
			}
		}
		return res;
	}

	/**
	 * 修改工地项目
	 *
	 * @param sysProject 工地项目
	 * @return 结果
	 */
	@Override
	public int updateSysProject(SysProject sysProject) {
		sysProject.setUpdateTime(DateUtils.getNowDate());
		sysProject.setUpdateBy(ShiroUtils.getLoginName());
		return sysProjectMapper.updateSysProject(sysProject);
	}

	/**
	 * 删除工地项目对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteSysProjectByIds(String ids) {
		return sysProjectMapper.deleteSysProjectByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除工地项目信息
	 *
	 * @param projectId 工地项目ID
	 * @return 结果
	 */
	@Override
	public int deleteSysProjectById(Long projectId) {
		return sysProjectMapper.deleteSysProjectById(projectId);
	}
	
	private String proCode() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		String code = "PRO-"+format.format(new Date());
		return code;
	}
	
	public static void main(String[] args) {
	}
}
